package com.lxf.clouduser.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.lxf.clouduser.bean.UserMsgBean;
import com.lxf.clouduser.mapper.UserMsgMapper;
import com.lxf.clouduser.service.UserMsgService;
import com.lxf.clouduser.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 罗兴发
 * @data 2025/6/18 15:36
 * @description：用户消息-业务实现层
 */
@Service
@Slf4j
public class UserMsgServiceImpl implements UserMsgService {
    @Resource
    UserMsgMapper userMsgMapper;
    @Resource
    private RedisUtils redisUtils;

    @Override
    public String getUserMsgById(JSONObject json, HttpServletRequest request) {
        JSONObject js = new JSONObject();

        //获取请求凭证,凭证中存放用户ID和用户名，存放格式为：userId_userName
        String secretToken = request.getHeader("secretToken");

        //获取redis中缓存的token
        Object o = redisUtils.get(secretToken);
        if (null!=o){
            String key = "getusermsgbyid";
            Object o1 = redisUtils.get(key);
            if (null!=o1){
                //json封装
                js.put("code",200);
                js.put("msg","Success");
                js.put("data",o1);
            }else {
                //消息ID
                Integer id = json.getInteger("id");
                UserMsgBean msg = userMsgMapper.getUserMsgById(id);

                //json封装
                js.put("code",200);
                js.put("msg","Success");
                js.put("data",msg);

                //redis缓存
                redisUtils.set(key,msg,3);

                //将消息更新为已读状态
                int status = 1;
                List<Integer> list = new ArrayList<>();
                list.add(id);
                userMsgMapper.updateMsgStatusById(list,status);
            }
        }else {
            js.put("code",400);
            js.put("msg","The Current Request is Invalid");
        }
        return JSONObject.toJSONString(js, SerializerFeature.WriteMapNullValue);
    }

    @Override
    public String updateMsgStatusById(JSONObject json, HttpServletRequest request) {
        JSONObject js = new JSONObject();

        //获取请求凭证,凭证中存放用户ID和用户名，存放格式为：userId_userName
        String secretToken = request.getHeader("secretToken");

        //获取redis中缓存的token
        Object o = redisUtils.get(secretToken);
        if (null!=o){
            //消息处理类型：1批量已读 2批量删除
            Integer mark = json.getInteger("mark");
            //消息id集合
            JSONArray ids = json.getJSONArray("ids");

            //JSONArray转List
            List<Integer> list = ids.stream().mapToInt(value1 -> Integer.parseInt(value1.toString())).boxed().collect(Collectors.toList());
            if (mark==1){

                //将消息更新为已读状态
                int status = 1;
                userMsgMapper.updateMsgStatusById(list, status);
            }else {

                //批量删除操作
                userMsgMapper.delMsgById(list);
            }

            //json封装
            js.put("code",200);
            js.put("msg","Success");

        }else {
            js.put("code",400);
            js.put("msg","The Current Request is Invalid");
        }
        return JSONObject.toJSONString(js, SerializerFeature.WriteMapNullValue);
    }
}
